<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="角色名称：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入角色名称"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="角色标识：" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入角色标识"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          clearable
          class="!w-[180px]"
        >
          <el-option label="已启用" value="1" />
          <el-option label="已停用" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-card>
      <template #header>
        <div class="card-header">
          <PlusForm
            :columns="searchColumns"
            :row-props="{ gutter: 20 }"
            submitText="搜索"
            resetText="重置"
            @submit="searchSubmit"
            @reset="searchReset"
          />
          <div style="padding-top: 20px">
            <el-button @click="handleOpen">添加</el-button>
          </div>
        </div>
      </template>
      <pure-table
        border
        row-key="id"
        alignWhole="center"
        adaptive
        showOverflowTooltip
        :loading="tableLoading"
        :data="tableData"
        :columns="tableColumns"
        :pagination="pagination"
        @page-size-change="onSizeChange"
        @page-current-change="onCurrentChange"
      >
        <template #operation="{ row }">
          <el-button link type="primary" size="small" @click="editClick(row)"
            >编辑</el-button
          >
        </template>
      </pure-table>
    </el-card>
    <PlusDialogForm
      v-model:visible="visible"
      v-model="values"
      :form="{ columns, rules }"
      :dialog="{ title: '添加组织' }"
      @confirm="submitForm"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import type { PlusColumn, FieldValues } from "plus-pro-components";
import type { PaginationProps, LoadingConfig, Align } from "@pureadmin/table";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { delay } from "@pureadmin/utils";

/** 搜索 */
const searchColumns: PlusColumn[] = [
  {
    label: "名称",
    width: 120,
    prop: "name",
    colProps: {
      span: 8
    }
  },
  {
    label: "状态",
    width: 120,
    prop: "status",
    valueType: "select",
    options: [
      {
        label: "未解决",
        value: "0",
        color: "red"
      },
      {
        label: "已解决",
        value: "1",
        color: "blue"
      },
      {
        label: "解决中",
        value: "2",
        color: "yellow"
      },
      {
        label: "失败",
        value: "3",
        color: "red"
      }
    ],
    colProps: {
      span: 8
    }
  }
];

const searchSubmit = (values: FieldValues) => {
  console.log(values, "Submit");
};
const searchReset = () => {
  console.log("handleReset");
};

/** 加载动画配置 */

const tableColumns: TableColumnList = [
  {
    type: "selection",
    align: "left",
    reserveSelection: true
  },
  {
    label: "名称",
    prop: "name",
    width: "260",
    fixed: true
  },
  {
    label: "类型",
    prop: "type_str",
    width: "260"
  },
  {
    label: "员工人数",
    prop: "staffCount",
    width: "260"
  },
  {
    label: "创建时间",
    prop: "createdDate",
    width: "260"
  },
  {
    label: "操作",
    width: "120",
    fixed: "right",
    slot: "operation"
  }
];
/** 分页配置 */
const pagination = reactive<PaginationProps>({
  pageSize: 10,
  currentPage: 1,
  pageSizes: [10, 15, 20],
  total: 0,
  align: "right",
  background: true,
  size: "default"
});
const page = ref(1);
const tableData = ref([]);
const tableLoading = ref(false);
const getTableData = async () => {
  if (tableLoading.value) {
    return false;
  }
  tableLoading.value = true;
  try {
    const response = await organizationsList({
      size: pagination.pageSize,
      page: page.value
    });
    tableData.value = response._embedded.organizationDtoes;
    tableLoading.value = false;
    pagination.total = response.page.totalElements;
    page.value = response.page.number + 1;
  } catch (error) {
    console.error("请求失败:", error);
  } finally {
    console.log("请求完成！");
    tableLoading.value = false;
  }
};
onMounted(() => {
  delay(600).then(() => {
    getTableData();
  });
});

function onSizeChange(val) {
  page.value = 1;
  pagination.pageSize = val;
  getTableData();
}
function onCurrentChange(val: number) {
  page.value = val;

  getTableData();
}
console.log(tableData);

//表单
const rules = {
  name: [
    {
      required: true,
      message: "请输入名称"
    }
  ],
  type: [
    {
      required: true,
      message: "请选择类型"
    }
  ],
  staffCount: [
    {
      required: true,
      message: "请输入员工人数"
    }
  ]
};

const columns: PlusColumn[] = [
  {
    label: "名称",
    width: 160,
    prop: "name"
  },
  {
    label: "类型",
    width: 160,
    prop: "type",
    valueType: "select",
    options: [
      {
        label: "党政机构",
        value: "PARTY_GOVERNMENT_OFFICE"
      }
    ]
  },
  {
    label: "员工人数",
    width: 160,
    prop: "staffCount"
  }
];
import { organizationsList, addOrganizations } from "@/api/zhaoyuan";
import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/utils";
const visible = ref(false);
const values = ref<FieldValues>({});

const handleOpen = () => {
  visible.value = true;
};

//编辑
const editClick = row => {
  console.log(row);
};
const submitForm = formData => {
  addOrganizations(formData).then(res => {});
  console.log(formData);
  http
    .request<any>("post", baseUrlApi("/api/organizations"), formData)
    .then(res => {
      console.log(res);
    });
};
</script>
